package users

import (
	"cloud_android_backend/pkg/models/orders"
	user "cloud_android_backend/pkg/models/users"
	"cloud_android_backend/pkg/utils"
	"github.com/golang/glog"
	"modules.tenxcloud.com/common/composite"
)

type User interface {
	GetType() string
	GetUserName() string
	GetCompanyID() string
}
type UserProfileResponse struct {
	UserName          string  `json:"username"`
	Email             string  `json:"email"`
	Phone             string  `json:"phone"`
	Balance           float64 `json:"balance"`
	FrozenBalance     float64 `json:"frozenBalance"`
	UsedCoupon        int     `json:"usedCoupon"`
	ShowTrialFinished bool    `json:"showTrialFinished"`
}

func GetUserInfo(userId string) (UserProfileResponse, error) {
	var userRes UserProfileResponse
	db, err := composite.Database.GetOrm()
	if err != nil {
		glog.Error(err)
		return userRes, err
	}
	userModel := user.User{UserID: userId}
	err = userModel.GetByUserId(db)
	if err != nil {
		glog.Error(err)
		return userRes, err
	}
	if userModel.UsedTrialCoupon == 1 {
		orderModel := orders.Orders{UserId: userId, PaymentSource: "trial"}
		count, err := orderModel.CountPurchesedForUsedCoupon(db, userId)
		if err != nil {
			glog.Error(err)

		}
		userRes.ShowTrialFinished = true
		if count > 0 {
			userRes.ShowTrialFinished = false
		}
	}
	userRes = UserProfileResponse{
		Phone:             utils.HidePhoneNumber(userModel.Phone),
		Balance:           userModel.Balance,
		FrozenBalance:     userModel.FrozenBalance,
		UserName:          userModel.UserName,
		Email:             userModel.Email,
		UsedCoupon:        userModel.UsedTrialCoupon,
		ShowTrialFinished: userRes.ShowTrialFinished,
	}
	return userRes, nil
}
